/*
 * Course.java
 * 
 * Copyright (C) 2011  Michael Palmer
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Version Changes:
 * May 22, 2011: 
 * Added isTaughtTime(Timeblock)
 * Added isTaughtWeekday
 * Added getWeekdays
 * Added setWeekdays
 * Modified Constructor() //Added timeblock handling and weekday handling
 * Added Constructor(params)
 * 
 * May 24, 2011:
 * Removed my_weekday and related methods
 * Changed constructor-> weekdays related
 * removed section
 * removed isTaughtTime and isTaughtWeekday
 * changed setWeekdays
 * 
 * May 29, 2011:
 * added isWithin
 * added equals
 * added hashCode
 * added toString
 * 
 */

package main;

/**
 * Represents a course offering at a college.
 * 
 * @author Michael Palmer
 * @version May 29, 2011
 */
public class Course {
	
	//Constants
    
    /**
     * The max number of students who can attend the course
     */
	public static final int MAX_STUDENTS = 30;
    /**
     * Total number of days in a week.
     */
	public static final int MAX_WEEKDAYS = 7;
    	
	//Fields

    /**
     * The human readable name of the course
     */
	private String my_name;
    /**
     * The course identification tag
     */
	private String my_id;
    /**
     * Name of the professor who is teaching
     */
	private String my_prof;
    /**
     * The section of this course
     */
	private String my_section;
    /**
     * Maximum number of students who can attend
     */
	private int my_student_max;
    /**
     * The time that this course is taught
     */
	private TimeBlock my_timeblock;
    /**
     * Number of credits offered for this course
     */
	private int my_credits;
 
	
	
	//Constructors
    
	/**
     * Constructs a "blank" Course object with no name,
     * teacher, id, etc.
     */
	public Course(){
		my_name = "";
		my_id = "";
		my_prof = "";
		my_section = "A";
		my_student_max = MAX_STUDENTS;		
		my_credits = 0;	
		
	}

    /**
     * Constructs a Course object with the given parameters
     * 
     * @param the_id Course identifier (eg. "TCSS 360")
     * @param the_section Course section
     * @param the_prof Name of the professor
     * @param the_title Human readable name
     * (eg. "Software Development and Quality Assurance")
     * @param the_days String of Days (eg. "MW")
     * @param the_startTime Starting time in 24 hour time
     * (eg. "1420" -> 2:20 PM)
     * @param the_endTime Ending time in 24 hour time
     * @param the_credits Number of credits received for
     * taking this course
     */
	public Course(final String the_id, 
	    final String the_section, final String the_title,
	    final String the_prof, final String the_days,
	    final String the_startTime, final String the_endTime,
	    final int the_credits){
	  
	    my_student_max = MAX_STUDENTS;
	    my_id = the_id;
	    my_section = the_section;
	    my_name = the_title;
	    my_prof = the_prof;

	    my_credits = the_credits;
	    
	    //Build the timeBlock
	    my_timeblock = new TimeBlock(the_startTime, the_endTime, the_days);
	    
	    //Set the weekdays
	    setWeekdays(the_days);	    

        my_student_max = MAX_STUDENTS;  
	}
	
	
	//Methods
    /**
     * Sets the human readable name of this course.
     * 
     * @param the_name the name of the course
     * (eg. "Software Development and Quality Assurance")
     */
	public void setTitle(final String the_name){
	  my_name = the_name;
	}

    /**
     * Sets the Identifier of this course.
     * 
     * @param the_id String representation of this course
     * (eg. "TCSS 360")
     */
	public void setName(final String the_id){
	  my_id = the_id;
	}

    /**
     * Sets the name of the professor who is
     * teaching this course
     * 
     * @param the_prof Name of the professor.
     */
	public void setProf(final String the_prof){
		my_prof = the_prof;
	}

    /**
     * Sets the course section
     * 
     * @param the_section A character (eg. A)
     */
	public void setSection(final String the_section){
		my_section = the_section;
	}

    /**
     * Sets the maximum number of students allowed
     * to attend this course
     * 
     * @param the_max the maximum number of students
     */
	public void setStudMax(final int the_max){
		my_student_max = the_max;
	}

    /**
     * Sets the credits received for taking this course
     * 
     * @param the_credits The number of credits
     */
	public void setCredits(final int the_credits){
	  my_credits = the_credits;
	}

    /**
     * Sets the weekdays that this course is taught
     * 
     * @param the_weekdays a String of weekday 
     * abbreviations (M, T, W, R, F, S)
     */
	public void setWeekdays(final String the_weekdays){
	  this.my_timeblock.setWeekDays(the_weekdays);
	}
	
    /**
     * Sets the time block for the course with
     * starting time and ending time.
     * 
     * @param the_startTime string representation of 
     * the course starting time
     * @param the_endTime string representation of
     * the course ending time
     */
	public void setTimeblock(final String the_startTime,
	    final String the_endTime, final String the_days)
	{
	  my_timeblock = new TimeBlock(the_startTime, the_endTime, the_days);
	}
	
    /**
     * Returns the human readable name of the course
     * (eg. "Software Development and Quality Assurance")
     * 
     * @return my_name
     */
	public String getTitle(){
	  return my_name;
	}

    /**
     * Returns the identifier of the course
     * (eg. "TCSS 360")
     * 
     * @return my_id
     */
	public String getName(){
		
		return my_id;		
	};

    /**
     * Returns the name of the professor who is teaching
     * the class
     * 
     * @return my_prof The professor's name.
     */
	public String getProf(){
		
		return my_prof;		
	}

  

    /**
     * Returns the section of this class.
     * 
     * @return my_section A string
     */
	public String getSection(){
		
		return my_section;
	}

    /**
     * Returns the max students for this course
     * 
     * @return my_student_max
     */
	public int getStudMax(){
		
		return my_student_max;
	}

    /**
     * Returns the timeblock in which this class
     * is taught
     * 
     * @return my_timeblock
     */
    public TimeBlock getTimeblock(){
      return my_timeblock;
    }

    /**
     * Returns the amount of credits for this course
     * 
     * @return my_credits the number of credits for 
     * this course
     */
	public int getCredits(){
	  return my_credits;
	}

    /**
     * Tells whether or not this class is the same class
     * as the given class.  Just tests the course ID
     * Preconditions: this course has been
     *  properly constructed, other_course has been
     *  properly constructed
     * Postconditions: this Course not changed
     * other_course not changed
     * @param the_otherCourse The other course
     * @return isEqual Whether or not same course
     */
	public boolean isEqual(final Course the_otherCourse){
		return this.my_id.equals(the_otherCourse.getName());	
	}

    /**
     * Tells whether this course is an exact copy
     * of the given course.
     * Preconditions: Both courses have been properly
     * constructed
     * Postconditions: Neither object will be changed
     * @param the_otherCourse The other course
     * @return isDuplicate Whether duplicate or not
     */
	public boolean isDuplicate(final Course the_otherCourse){

		return (my_id.equals(the_otherCourse.getName())
		   && my_prof.equals(the_otherCourse.getProf())
		   && my_timeblock.equals(the_otherCourse.getTimeblock()));
	}	
	
	/**
	 * Tells whether this course is within a given
	 *  time block.
	 * Preconditions: this course has been properly 
	 * constructed
	 * Postconditions: Neither object will be changed
	 * @param the_timeblock The time to check
	 * @return isWithin Whether within or not
	 */
	public boolean isWithin(final TimeBlock the_timeblock){
	  return my_timeblock.isWithin(the_timeblock);
	}
	
	/**
	 * The overwritten equals method
	 * Preconditions: This object is constructed properly
	 * Postconditions: False if the other object is not
	 * the same.  True if the same object.
	 */	
	@Override
	public boolean equals(Object the_other) {
      boolean result = false;
      
      if (the_other != null &&
          the_other instanceof Course)
      {
        Course other = (Course) the_other;
        result = my_id.equals(other.getName());
      }
      return result;
  }
  
  /**
   * Overwritten HashCode method.
   */
  public int hashCode() {
      return this.getTimeblock().hashCode();
  }
   
  /**
   * {@inheritDoc}
   */
  @Override
  public String toString()
  {
      StringBuilder sb = new StringBuilder("Course ID:\t");
      sb.append(this.getName());
      sb.append("\nName:\t\t");
      sb.append(this.getTitle());
      sb.append("\nSection:\t");
      sb.append(this.getSection());
      sb.append("\nProfessor:\t");
      sb.append(this.getProf());
      sb.append("\nDays:\t\t");
      sb.append(this.getTimeblock().getWeekDays());
      sb.append("\nTime:\t\t");
      sb.append(this.getTimeblock().getStartHour());
      sb.append(":");
      sb.append(this.getTimeblock().getStartMinute());
      sb.append(" - ");
      sb.append(getTimeblock().getEndHour());
      sb.append(":");
      sb.append(getTimeblock().getEndMinute());
      return sb.toString();
  }
	
}
